<template>
  <div class="uni-scroll" style="height: calc(100vh - 150px);">
    <el-form
      v-if="company"
      label-width="120px"
    >
      <el-form-item :label="$t('devicePublic')">
        <el-switch
          v-model="company.devicePublic"
          :active-text="$t('open')"
          :inactive-text="$t('close')"
        />
      </el-form-item>
      <el-form-item :label="$t('server')+$t('linkage')">
        <el-switch
          v-model="company.enableServerLinkage"
          :active-text="$t('open')"
          :inactive-text="$t('close')"
        />
      </el-form-item>
      <el-form-item :label="$t('save')">
        <el-button type="primary" @click="updateCompanyConfigDevice">{{ $t('confirm') }}</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>

import {
  successNotice
} from '@/util/common'

import {
  updateCompanyConfigDevice
} from '@/api/iot/iotCompanyConfigDeviceApi'
import {
  getCompanyConfigDevice
} from '@/api/iot/iotCompanyConfigDeviceApi'

export default {
  name: 'IotConfigDevice',
  props: {
    companyId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      company: {}
    }
  },
  created() {
    this.getCompanyConfigDevice()
  },
  methods: {
    getCompanyConfigDevice() {
      getCompanyConfigDevice({
        companyId: this.companyId
      }).then(res => {
        const { code, data } = res
        if (code === '200') {
          this.company = data
        }
      })
    },
    updateCompanyConfigDevice() {
      updateCompanyConfigDevice({
        ...this.company,
        companyId: this.companyId
      }).then(res => {
        const { code } = res
        if (code === '200') {
          successNotice(this)
          this.getCompanyWithoutToken()
        }
      })
    }
  }
}
</script>

